﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Data.Linq;
using System.Collections.Generic;

namespace Hisun.View.ReportQuery
{
    public partial class Report_ZJCBFPCX : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alert;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AbcFullAccountPeriod MyFullAp = new AbcFullAccountPeriod();

                MyFullAp.FullAccountPeriodToDropdownlist(DropDownList_Year, DropDownList_Month, Application["accountPeriod"].ToString());

                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();
                DropDownList_CenterList.Items.Clear();
                FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));


            }
        }


        protected void Button_fullCenterList_Click(object sender, EventArgs e)
        {
            var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();
            DropDownList_CenterList.Items.Clear();
            ListItem InsertItem = new ListItem("请选择", "");
            DropDownList_CenterList.Items.Add(InsertItem);
            FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

        }
        //查询当前操作员对应的作业中心列表
        private void FillData2DropDownList(int CenterID)
        {
            ChildCenterList myCenter = new ChildCenterList(CenterID);

            var myCenterFilters = myCenter.CostCenterlist.Where(p => p.是否直接成本 == true && p.停用 == false );
            foreach (var MylistItem in myCenterFilters)
            {
                if (MylistItem.作业中心名称.Contains(TextBox_FilterText.Text))
                {
                    ListItem InsertItem = new ListItem(MylistItem.作业中心名称, MylistItem.作业中心ID.ToString());
                    DropDownList_CenterList.Items.Add(InsertItem);
                }
            }


        }

        


        //点击查询
        protected void Button_Query_Click(object sender, EventArgs e)
        {

            string AccountPeriod = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); ;




            int MyCostCenterId = Convert.ToInt32(DropDownList_CenterList.SelectedValue);


            var IsValidString = from a in db.TABC_JS_AccountPeriods
                                where a.会计期间ID == AccountPeriod
                                select a;
            if (IsValidString.Count() == 0)
            {
                Response.Write("<Script>alert('会计期间不存在，请检查输入是否正确')</Script>");
                return;
            }
            else if (IsValidString.Count() == 1)
            {

                string CurrentUser = Session[ServerVars.sysSessionUserName].ToString();
                CostReport MyReport = new CostReport();
                string myHtmlReport = "";
                myHtmlReport = MyReport.WriteExcel_Report_ZJCBFPCX(MyCostCenterId, AccountPeriod, CurrentUser);
                Response.Redirect("/Report/UserReport/" + myHtmlReport + ".html");

            }
        }


        //点击打印
        protected void Button_Printe_Click(object sender, EventArgs e)
        {
            string AccountPeriod = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); ;


            int MyCostCenterId = Convert.ToInt32(DropDownList_CenterList.SelectedValue);

            string MyCenterName = DropDownList_CenterList.SelectedItem.Text;



            var IsValidString = from a in db.TABC_JS_AccountPeriods
                                where a.会计期间ID == AccountPeriod
                                select a;
            if (IsValidString.Count() == 0)
            {
                Response.Write("<Script>alert('会计期间不存在，请检查输入是否正确')</Script>");
                return;
            }
            else if (IsValidString.Count() == 1)
            {

                string CurrentUser = Session[ServerVars.sysSessionUserName].ToString();
                CostReport MyReport = new CostReport();
                string myHtmlReport = "";
                myHtmlReport = MyReport.WriteExcel_Report_ZJCBFPCX(MyCostCenterId, AccountPeriod, CurrentUser);


                HttpResponse response = HttpContext.Current.Response;
                response.Clear();
                response.WriteFile("/Report/UserReport/" + myHtmlReport + ".xls");
                string httpHeader = "attachment;filename= ZJCBFPCX(" + AccountPeriod + ")" + ".Xls";
                response.AppendHeader("Content-Disposition", httpHeader);
                response.Flush();


            }
        }

    }
}
